package com.sy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.blog.dao.mapper.SysUserMapper;
import com.sy.blog.dao.pojo.Article;
import com.sy.blog.dao.pojo.SysUser;
import com.sy.blog.service.LoginService;
import com.sy.blog.service.SysUserService;
import com.sy.blog.vo.ErrorCode;
import com.sy.blog.vo.LoginUserVo;
import com.sy.blog.vo.Result;
import com.sy.blog.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private LoginService loginService;

    /**
     * 1、检验token是否合法
     *      非空、能否解析
     * 2、校验失败，返回失败
     * 3、校验成功，返回UserVo对象
     * @param token
     * @return
     */
    @Override
    public Result findUserByToken(String token) {
        SysUser sysUser = loginService.checkToken(token);
        if (sysUser == null) {
            return Result.fail(ErrorCode.TOKEN_NOT_EXIST.getCode(), ErrorCode.TOKEN_NOT_EXIST.getMsg());
        }

        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setId(String.valueOf(sysUser.getId()));
        loginUserVo.setNickname(sysUser.getNickname());

        return Result.success(loginUserVo);
    }

    @Override
    public SysUser findUserByArticleId(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        if (sysUser == null) {
            SysUser sysUser1 = new SysUser();
            sysUser1.setNickname("神勇就是我");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account);
        queryWrapper.eq(SysUser::getPassword, password);
        queryWrapper.select(SysUser::getId, SysUser::getAccount, SysUser::getAvatar, SysUser::getNickname);
        // 查到一条就停止，不然会一直查下去影响效率
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        // mybatis-plus默认生成的id是分布式id（雪花算法），而不是自增id
        sysUserMapper.insert(sysUser);
    }

    @Override
    public UserVo findUserVoByAuthorId(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        UserVo userVo = new UserVo();
        if (sysUser == null) {
            userVo.setId("1");
            userVo.setNickname("神勇就是我");
            userVo.setAvatar("/static/img/logo.b3a48c0.png");
        }

        userVo.setAvatar(sysUser.getAvatar());
        userVo.setNickname(sysUser.getNickname());
        userVo.setId(String.valueOf(sysUser.getId()));
        return userVo;
    }

}
